package com.zzp.sms.security.config;

import com.zzp.sms.security.filter.SmsCodeFilter;
import com.zzp.sms.security.handler.CustomizeAuthenticationFailureHandler;
import com.zzp.sms.security.handler.CustomizeAuthenticationSuccessHandler;
import com.zzp.sms.security.handler.NoPermitAccessHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @author 佐斯特勒
 * <p>
 *  web安全配置类
 * </p>
 * @version v1.0.0
 * @date 2020/1/11 17:13
 * @see  WebSecurityConfiguration
 **/
@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {


    /**
     * 短信验证码过滤
     */
    @Resource
    private SmsCodeFilter smsCodeFilter;

    /**
     * 失败认证处理
     */
    @Resource
    private CustomizeAuthenticationFailureHandler authenticationFailureHandler;

    /**
     * 成功认证处理
     */
    @Resource
    private CustomizeAuthenticationSuccessHandler authenticationSuccessHandler;

    /**
     * sms码认证
     */
    @Resource
    private SmsCodeAuthenticationConfig smsCodeAuthenticationConfig;

    /**
     * 无权限异常访问处理
     */
    @Resource
    private NoPermitAccessHandler noPermitAccessHandler;


    @Bean
    public BCryptPasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    /**
     * 数据源
     */
    @Resource
    private DataSource dataSource;
    /*@Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource(){
        return DataSourceBuilder.create().build();
    }*/

    /**
     * 持久化token 基于jdbc
     * @return {@link JdbcTokenRepositoryImpl}
     */
    @Bean
    public PersistentTokenRepository persistentTokenRepository(){
        var jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
        return jdbcTokenRepository;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                // 过滤器先行
            .addFilterBefore(smsCodeFilter, UsernamePasswordAuthenticationFilter.class)
                .formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/login")
                .failureHandler(authenticationFailureHandler)
                .successHandler(authenticationSuccessHandler)
                .and()
                // 认证请求
                .authorizeRequests()
                // 无需认证的资源
                .antMatchers("/login.html", "/smsCode", "/css/**", "/js/**").permitAll()
                //基于操作
                .antMatchers("/company/delete").hasAuthority("company:delete")
                .antMatchers("/company/add").hasAuthority("company:add")
                .antMatchers("/company/edit").hasAuthority("company:edit")
                // 应用到所有请求
                .anyRequest()
                // 验证后可通过
                .authenticated()
                .and()
                .logout()
                .logoutSuccessUrl("/login.html")
                .and()
                .exceptionHandling()
                //无权限访问处理
                .accessDeniedHandler(noPermitAccessHandler)
                .and()
                .csrf().disable()
                //将短信验证的调用链路应用到spring security中
                .apply(smsCodeAuthenticationConfig);
    }
}
